﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SolverLibrary.Data_Entities
{
    public class SudokuPuzzle : Puzzle
    {
        static bool done = false;

        SudokuBoard board = new SudokuBoard();

        public SudokuPuzzle()
        {
            // do nothing, board is already initialized
        }

        public SudokuPuzzle(Board board)
        {
            this.board = (SudokuBoard)board;
        }

        public Board getBoard()
        {
            return this.board;
        }


        public Board solve()
        {
            populateBoard();
            return board;
        }

        public void populateBoard()
        {
            if (board.isFull())
            {
                done = true;
                return;
            }
            else
            {
                Candidate candidate = board.bestCandidate();
                foreach (int candidateSolution in candidate.Candidates)
                {
                    board.add(SudokuBoardItem.getSudokuBoardItem(
                        candidateSolution),
                        candidate.Column,
                        candidate.Row);
                    populateBoard();

                    if (!done)
                    {
                        board.remove(candidate.Column,candidate.Row);
                    }
                }
            }
        }
    }
}
